import configparser

class Config():
    
    logFormat = None
    logDateTimeFormat = None
    logLevel = None
    logFile = None
    host = None
    port = None
    bufferSize = None
    authMethod = None
    qrImg = None
    cfgData = None
    
    blacklist = None
    
    @classmethod
    def __init__(cls):
        #cls.config = configparser.ConfigParser()
        config = configparser.RawConfigParser()
        config.read('LinRemoteServer.cfg')
        cls.cfgData = config

        try:
            cls.logFormat = config['log']['format']
        except (KeyError, ValueError):
            cls.logFormat = '[%(asctime)s.%(msecs)03d] %(levelname)s: %(message)s'
        
        try:
            cls.logDateTimeFormat = config['log']['dateTimeFormat']
        except (KeyError, ValueError):
            cls.logDateTimeFormat = '%Y-%m-%d %I:%M:%S'
        
        try:
            cls.logLevel = config['log']['level']
        except (KeyError, ValueError):
            cls.logLevel = 'INFO'
        
        try:
            cls.logFile = config['log']['file']
        except (KeyError, ValueError):
            cls.logFile = 'linRemoteServer.log'
            
        try:
            cls.host = config['socket']['host']
        except (KeyError, ValueError):
            cls.host = ''
        try:
            cls.port = int(config['socket']['port'])
        except (KeyError, ValueError):
            cls.port = 5005
        try:
            cls.bufferSize = int(config['socket']['bufferSize'])
        except (KeyError, ValueError):
            cls.bufferSize = 4096
        try:
            cls.authMethod = config['authorization']['method']
        except (KeyError, ValueError):
            cls.authMethod = 'simple'
        if cls.authMethod == 'qrcode':
            try:
                cls.qrImg = config['authorization']['qrImg']
            except (KeyError, ValueError):
                cls.qrImg = '/tmp/lr.png'
        try:
            cls.blacklist = str(config['blacklist']['IPlist'])
        except (KeyError, ValueError):
            cls.blacklist = ''

    @classmethod
    def reload(cls):
        cls.authMethod = None
        cls.qrImg = None
        cls.cfgData = None
        cls.blacklist = None
        config = configparser.RawConfigParser()
        config.read('LinRemoteServer.cfg')
        cls.cfgData = config
        try:
            cls.authMethod = config['authorization']['method']
        except (KeyError, ValueError):
            cls.authMethod = 'simple'
        if cls.authMethod == 'qrcode':
            try:
                cls.qrImg = config['authorization']['qrImg']
            except (KeyError, ValueError):
                cls.qrImg = '/tmp/lr.png'
        try:
            cls.blacklist = str(config['blacklist']['IPlist'])
        except (KeyError, ValueError):
            cls.blacklist = ''

    @classmethod 
    def changeVariable(cls, name, value):
        setattr(cls, name, value)